package com.supermarket.view;

import com.supermarket.Main;
import com.supermarket.entity.SupplierEntity;
import com.supermarket.serivce.SupplierSerivce;
import com.supermarket.utils.ScannerUtil;

import java.util.ArrayList;

public class SupplierView {
    //创建SupplierService对象
    private SupplierSerivce supplierService = new SupplierSerivce();
    //主窗口
    public void SupplierView(){
        while (true) {
            System.out.println("=======供应商管理=======");
            System.out.println("1. 查询所有供应商信息");
            System.out.println("2. 根据ID查询供应商信息");
            System.out.println("3. 新增供应商信息");
            System.out.println("4. 根据ID修改供应商信息");
            System.out.println("5. 删除供应商");
            System.out.println("0. 返回上一级菜单");
            System.out.println("=======供应商管理=======");
            System.out.println("请输入菜单序号：");
            int result = ScannerUtil.getInt();
            ScannerUtil.getLine();
            switch (result) {
                case 1:
                    showAllSuppliers();
                    break;
                case 2:
                    findSupplierById();
                    break;
                case 3:
                    insertSupplier();
                    break;
                case 4:
                    updateSupplier();
                    break;
                case 5:
                    deleteSupplier();
                    break;
                case 0:
                    Main.mainMenu();
                default:
                    System.out.println("您输入的菜单序号不正确，请重新输入!");
                    break;
            }
        }
    }
    //窗口 - 查询所有供应商信息
    public void showAllSuppliers() {
        System.out.println("===========供应商列表===========");
        ArrayList<SupplierEntity> suppliers = supplierService.allSuppliers();
        if (suppliers.isEmpty()) {
            System.out.println("当前没有供应商信息。");
            System.out.println("===========供应商列表===========");
        } else {
            System.out.println("所有供应商信息如下：");
            for (SupplierEntity supplier : suppliers) {
                System.out.println(supplier);
            }
            System.out.println("===========供应商列表===========");
        }
    }

    //窗口 - 根据ID查询供应商信息
    public void findSupplierById() {
        System.out.println("===========供应商查询===========");
        System.out.print("请输入供应商ID：");
        Long supplierId = ScannerUtil.getLong();
        SupplierEntity supplier = supplierService.getSupplierById(supplierId);
        if (supplier == null) {
            System.out.println("未找到ID为 " + supplierId + " 的供应商。");
            System.out.println("===========供应商查询===========");
        } else {
            System.out.println("供应商信息：");
            System.out.println(supplier);
            System.out.println("===========供应商查询===========");
        }
    }

    //窗口 - 新增供应商信息
    public void insertSupplier() {
        System.out.println("===========添加供应商===========");
        System.out.print("请输入供应商名称：");
        String name = ScannerUtil.getLine();
        System.out.print("请输入供应商地址：");
        String address = ScannerUtil.getLine();
        System.out.print("请输入供应商电话：");
        String phone = ScannerUtil.getLine();

        // 创建 SupplierEntity 对象，id 为 null
        SupplierEntity supplier = new SupplierEntity(null, name, address, phone);

        // 插入供应商信息
        int result = supplierService.insertSupplier(supplier);
        if (result > 0) {
            System.out.println("供应商信息插入成功。");
            System.out.println("===========添加供应商===========");
        } else {
            System.out.println("供应商信息插入失败。");
            System.out.println("===========添加供应商===========");
        }
    }

    //窗口 - 根据ID修改供应商信息
    public void updateSupplier() {
        System.out.println("===========修改供应商信息===========");
        System.out.print("请输入供应商ID：");
        Long supplierId = ScannerUtil.getLong();
        ScannerUtil.getLine(); // 捕捉换行符

        SupplierEntity supplier = supplierService.getSupplierById(supplierId);
        if (supplier == null) {
            System.out.println("未找到ID为 " + supplierId + " 的供应商。");
            System.out.println("===========修改供应商信息===========");
            return;
        }
        System.out.print("请输入新的供应商名称（当前：" + supplier.getName() + "）：");
        String name = ScannerUtil.getLine();
        System.out.print("请输入新的供应商地址（当前：" + supplier.getAddress() + "）：");
        String address = ScannerUtil.getLine();
        System.out.print("请输入新的供应商电话（当前：" + supplier.getPhone() + "）：");
        String phone = ScannerUtil.getLine();
        supplier.setName(name);
        supplier.setAddress(address);
        supplier.setPhone(phone);
        int result = supplierService.updateSupplier(supplier);
        if (result > 0) {
            System.out.println("供应商信息更新成功。");
            System.out.println("===========修改供应商信息===========");
        } else {
            System.out.println("供应商信息更新失败。");
            System.out.println("===========修改供应商信息===========");
        }
    }

    // 窗口 - 删除供应商
    public void deleteSupplier() {
        System.out.println("===========删除供应商===========");
        System.out.println("请输入您要删除的供应商ID：");
        Long supplierId = ScannerUtil.getLong();
        ScannerUtil.getLine();

        SupplierEntity supplier = supplierService.getSupplierById(supplierId);
        if (supplier == null) {
            System.out.println("未找到ID为 " + supplierId + " 的供应商。");
            System.out.println("===========删除供应商===========");
            return;
        }
        System.out.println("您确定要删除该供应商吗？(Y/N)");
        char key = ScannerUtil.getString().charAt(0);
        if(key == 'Y' || key == 'y'){
            int result = supplierService.deleteSupplier(supplier);
            if (result > 0) {
                System.out.println("供应商信息更新成功。");
                System.out.println("===========删除供应商===========");
            } else {
                System.out.println("供应商信息更新失败。");
                System.out.println("===========删除供应商===========");
            }
        } else if (key == 'N' || key == 'n'){
            return;
        }

    }
}
